package com.controller.backend;

import com.alibaba.fastjson.JSON;
import com.biz.CustomerBiz;
import com.biz.SysOperatingLogBiz;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.Customer;
import com.entity.model.ManagerInfo;
import com.entity.model.SysOperatingLog;
import com.github.pagehelper.PageInfo;
import com.util.CalendarUtil;
import com.util.SystemConstant;
import com.util.webpage.PageData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Condition;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by jc on 2019/11/16.
 */
@Controller
@RequestMapping("/backend/security/customer")
public class CustomerController extends BaseController<Customer> {
    @Autowired
    private CustomerBiz customerBiz;

    @Autowired
    private SysOperatingLogBiz sysOperatingLogBiz;

    //用户列表
    @RequestMapping(value = "index")
    public String index() throws Exception {
        return "/backend/security/customer/list";
    }

    //用户详情信息
    @RequestMapping(value = "detail")
    public String detail() throws Exception {
        return "/backend/security/customer/form";
    }

    @RequestMapping(value = "promotionList")
    public String promotionList() throws Exception {
        return "/backend/security/customer/promotionList";
    }

    @RequestMapping(value = "moreVipForm")
    public String moreVipForm() throws Exception {
        return "/backend/security/customer/moreVipForm";
    }

    //上传用户头像
    @RequestMapping(value = "uploadImage")
    public String uploadImage() throws Exception {
        return "/backend/security/photoClip/photoClip";
    }

    @RequestMapping(value = "addCustomer")
    @ResponseBody
    public Result addCustomer(Customer customer, HttpSession session) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);

        SysOperatingLog sysOperatingLog = new SysOperatingLog();
        sysOperatingLog.setBeforeLog(null);
        sysOperatingLog.setOperatingId(managerInfo.getId());
        sysOperatingLog.setOperatingName(managerInfo.getUserName());

        Condition cond = new Condition(Customer.class);
        cond.createCriteria().andEqualTo("account", customer.getAccount());
        if (customerBiz.findByCondition(cond).size() < 1) {
            customerBiz.save(customer);

            sysOperatingLog.setAfterLog(JSON.toJSONString(customer));
            sysOperatingLog.setRemark("新增用户");
            sysOperatingLogBiz.save(sysOperatingLog);
            return ResultGenerator.genSuccessResult(customer);
        }
        return ResultGenerator.genFailResult("手机号码已存在");
    }

    @RequestMapping(value = "updateCustomer")
    @ResponseBody
    public Result updateCustomer(Customer customer, HttpSession session) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);

        SysOperatingLog sysOperatingLog = new SysOperatingLog();
        sysOperatingLog.setBeforeLog(JSON.toJSONString(customer));
        sysOperatingLog.setOperatingId(managerInfo.getId());
        sysOperatingLog.setOperatingName(managerInfo.getUserName());

        customerBiz.update(customer);

        sysOperatingLog.setAfterLog(JSON.toJSONString(customer));
        sysOperatingLog.setRemark("修改用户");
        sysOperatingLogBiz.save(sysOperatingLog);

        return ResultGenerator.genSuccessResult(customer);
    }

    @RequestMapping(value = "customerList")
    @ResponseBody
    public Result customerList(HttpServletRequest request) {
        PageData cond = new PageData(request);
        List<Customer> list = customerBiz.customerList(cond);
        PageInfo<Customer> pageInfo = new PageInfo<Customer>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }

    @RequestMapping(value = "pdCustomerList")
    @ResponseBody
    public Result pdCustomerList(HttpServletRequest request) {
        PageData cond = new PageData(request);
        List<Customer> list = customerBiz.pdCustomerList(cond);
        PageInfo<Customer> pageInfo = new PageInfo<Customer>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }

    @RequestMapping(value = "updateVipDay")
    @ResponseBody
    public Result updateVipDay(Integer id, Integer days, HttpSession session) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);

        Customer customer = customerBiz.findById(id);

        SysOperatingLog sysOperatingLog = new SysOperatingLog();
        sysOperatingLog.setBeforeLog(JSON.toJSONString(customer));
        sysOperatingLog.setOperatingId(managerInfo.getId());
        sysOperatingLog.setOperatingName(managerInfo.getUserName());

        if (customer.getIsVip() > 1 && customer.getVipEndDate() != null) {
            customer.setVipEndDate(CalendarUtil.getAddDays2(customer.getVipEndDate(), days));
            customerBiz.update(customer);

            sysOperatingLog.setAfterLog(JSON.toJSONString(customer));
            sysOperatingLog.setRemark("修改用户会员剩余时间");
            sysOperatingLogBiz.save(sysOperatingLog);
        }
        return ResultGenerator.genSuccessResult();
    }

}
